﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using NangLuongTieuDiemDTO;
using NangLuongTieuDiemBUS;
using NangLuongTieuDiemDTO.DTO;
using NangLuongTieuDiemBUS.BUS;
using NangLuongTieuDiemBUS.SDK;
using DevExpress.Utils.Menu;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Menu;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Controls;
using System.Text.RegularExpressions;


namespace NangLuongTieuDiemGUI.NangLuongTieuDiemUserControl
{
    public partial class ucDanhSachSanPham : UserControl
    {
        string dataOfCell;
        bool isNumberic = true;
        public ucDanhSachSanPham()
        {
            InitializeComponent();
        }

        #region "[ucDanhSachSanPham Load]"// Cao Hoang An - 1241301
        private void ucDanhSachSanPham_Load(object sender, EventArgs e)
        {
            try
            {
                LoadLoaiSanPham();
                LoadSanPham();
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("ucDanhSachSanPham_Load", ex);
            }
        }
        #endregion

        #region "[btnTimKiem Click]"// Cao Hoang An - 1241301
        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            try{
                try
                {
                    int.Parse(cbTimKiem.Text);
                    isNumberic = true;
                }
                catch
                {
                    isNumberic = false; //Cho nay ko can insert loi
                }

                //Lay gia tri cua cbTimKiem
                dataOfCell = cbTimKiem.Text.ToLower();

                //clear dataGridview
                dgvPin.DataSource = null;
                dgvAcQuy.DataSource = null;
                dgvBoDieuKhienSac.DataSource = null;
                dgvThietBiDien.DataSource = null;

                //Tim kiem theo LoaiSanPhamID
                if (cbTimKiem.SelectedIndex != -1)
                {
                    dgvPin.Visible = dgvAcQuy.Visible = dgvBoDieuKhienSac.Visible = dgvThietBiDien.Visible = true;
                    TimKiemTheoLoaiSanPhamID(cbTimKiem.SelectedIndex);
                }
                else//Tim kiem theo Nguoi Dung nhap vao
                {
                    if (cbTimKiem.Text == "")
                    {
                        MessageBox.Show("Bạn chưa nhập nội dung bạn cần tìm kiếm.", "Thông báo:");
                        return;
                    }
                    else
                    {
                        dgvPin.Visible = dgvAcQuy.Visible = dgvBoDieuKhienSac.Visible = dgvThietBiDien.Visible = true;
                        TimKiemTheoNguoiDungNhapVao(isNumberic, cbTimKiem.Text);
                    }
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("btnTimKiem_Click", ex);
            }
        }
        #endregion

        #region "[Đánh số thứ tự cho dgvPin và tô màu gridview1]"// Cao Hoang An - 1241301
        private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            try
            {
                if (e.Column.Name == "clSTT")
                    e.DisplayText = (e.RowHandle + 1).ToString();
                if (isNumberic == true || cbTimKiem.SelectedIndex != 0)
                {
                    //LoaiSanPhamID
                    string dataLoaiSanPhamID = Convert.ToString(gridView1.GetRowCellValue(e.RowHandle, "LoaiSanPhamID"));
                    if (dataLoaiSanPhamID == cbTimKiem.SelectedIndex.ToString())
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "LoaiSanPhamID")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Dai
                    string dataDai = Convert.ToString(gridView1.GetRowCellValue(e.RowHandle, "Dai"));
                    if (dataDai == dataOfCell)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "Dai")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Rong
                    string dataRong = Convert.ToString(gridView1.GetRowCellValue(e.RowHandle, "Rong"));
                    if (dataRong == dataOfCell)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "Rong")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Day
                    string dataDay = Convert.ToString(gridView1.GetRowCellValue(e.RowHandle, "Day"));
                    if (dataDay == dataOfCell)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "Day")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Gia
                    string dataGia = Convert.ToString(gridView1.GetRowCellValue(e.RowHandle, "Gia"));
                    if (dataGia == dataOfCell)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "Gia")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Voc
                    string dataVoc = Convert.ToString(gridView1.GetRowCellValue(e.RowHandle, "Voc"));
                    if (dataVoc == dataOfCell)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "Voc")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Isc
                    string dataIsc = Convert.ToString(gridView1.GetRowCellValue(e.RowHandle, "Isc"));
                    if (dataIsc == dataOfCell)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "Isc")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Vmp
                    string dataVmp = Convert.ToString(gridView1.GetRowCellValue(e.RowHandle, "Vmp"));
                    if (dataVmp == dataOfCell)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "Vmp")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Imp
                    string dataImp = Convert.ToString(gridView1.GetRowCellValue(e.RowHandle, "Imp"));
                    if (dataImp == dataOfCell)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "Imp")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Cong Suat
                    string dataCongSuat = Convert.ToString(gridView1.GetRowCellValue(e.RowHandle, "CongSuat"));
                    if (dataCongSuat == dataOfCell)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "CongSuat")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                }

                if (isNumberic == false)
                {
                    //NguonNhap
                    bool isContainsNguonNhap = false;
                    string dataNguonNhap = Convert.ToString(gridView1.GetRowCellValue(e.RowHandle, "NguonNhap")).ToLower();
                    isContainsNguonNhap = dataNguonNhap.Contains(dataOfCell);
                    if (isContainsNguonNhap == true)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "NguonNhap")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Hinh Dang
                    bool isContainsHinhDang = false;
                    string dataHinhDang = Convert.ToString(gridView1.GetRowCellValue(e.RowHandle, "HinhDang")).ToLower();
                    isContainsHinhDang = dataHinhDang.Contains(dataOfCell);
                    if (isContainsHinhDang == true)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "HinhDang")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Ten SP
                    bool isContainsTenSP = false;
                    string dataTenSP = Convert.ToString(gridView1.GetRowCellValue(e.RowHandle, "TenSanPham")).ToLower();
                    isContainsTenSP = dataTenSP.Contains(dataOfCell);
                    if (isContainsTenSP == true)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "TenSanPham")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("gridView1_CustomDrawCell", ex);
            }
        }
        #endregion

        #region "[Đánh số thứ tự cho dgvAcQuy và tô màu gridview2]"// Cao Hoang An - 1241301
        private void gridView2_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            try
            {
                if (e.Column.Name == "clSTT2")
                    e.DisplayText = (e.RowHandle + 1).ToString();
                if (isNumberic == true || cbTimKiem.SelectedIndex != 0)
                {
                    //LoaiSanPhamID
                    string dataLoaiSanPhamID = Convert.ToString(gridView2.GetRowCellValue(e.RowHandle, "LoaiSanPhamID"));
                    if (dataLoaiSanPhamID == cbTimKiem.SelectedIndex.ToString())
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "LoaiSanPhamID")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Gia
                    string dataGia = Convert.ToString(gridView2.GetRowCellValue(e.RowHandle, "Gia"));
                    if (dataGia == dataOfCell)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "Gia")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Cong Suat (Sô AH)
                    string dataCongSuat = Convert.ToString(gridView2.GetRowCellValue(e.RowHandle, "CongSuat"));
                    if (dataCongSuat == dataOfCell)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "CongSuat")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Hieu điên thê (vpm)
                    string dataVmp = Convert.ToString(gridView2.GetRowCellValue(e.RowHandle, "Vmp"));
                    if (dataVmp == dataOfCell)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "Vmp")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                }

                if (isNumberic == false)
                {
                    //NguonNhap
                    bool isContainsNguonNhap = false;
                    string dataNguonNhap = Convert.ToString(gridView2.GetRowCellValue(e.RowHandle, "NguonNhap")).ToLower();
                    isContainsNguonNhap = dataNguonNhap.Contains(dataOfCell);
                    if (isContainsNguonNhap == true)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "NguonNhap")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Ten SP
                    bool isContainsTenSP = false;
                    string dataTenSP = Convert.ToString(gridView2.GetRowCellValue(e.RowHandle, "TenSanPham")).ToLower();
                    isContainsTenSP = dataTenSP.Contains(dataOfCell);
                    if (isContainsTenSP == true)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "TenSanPham")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("gridView2_CustomDrawCell", ex);
            }
        }
        #endregion

        #region "[Đánh số thứ tự cho dgvBoDieuKienSac và tô màu gridview3]"// Cao Hoang An - 1241301
        private void gridView3_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            try
            {
                if (e.Column.Name == "clSTT3")
                    e.DisplayText = (e.RowHandle + 1).ToString();
                if (isNumberic == true || cbTimKiem.SelectedIndex != 0)
                {
                    //LoaiSanPhamID
                    string dataLoaiSanPhamID = Convert.ToString(gridView3.GetRowCellValue(e.RowHandle, "LoaiSanPhamID"));
                    if (dataLoaiSanPhamID == cbTimKiem.SelectedIndex.ToString())
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "LoaiSanPhamID")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Gia
                    string dataGia = Convert.ToString(gridView3.GetRowCellValue(e.RowHandle, "Gia"));
                    if (dataGia == dataOfCell)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "Gia")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Imp (Ampe)
                    string dataImp = Convert.ToString(gridView3.GetRowCellValue(e.RowHandle, "Imp"));
                    if (dataImp == dataOfCell)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "Imp")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Voc (VMax)
                    string dataVoc = Convert.ToString(gridView3.GetRowCellValue(e.RowHandle, "Voc"));
                    if (dataVoc == dataOfCell)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "Voc")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Vmp (VMin)
                    string dataVmp = Convert.ToString(gridView3.GetRowCellValue(e.RowHandle, "Vmp"));
                    if (dataVmp == dataOfCell)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "Vmp")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                }

                if (isNumberic == false)
                {
                    //NguonNhap
                    bool isContainsNguonNhap = false;
                    string dataNguonNhap = Convert.ToString(gridView3.GetRowCellValue(e.RowHandle, "NguonNhap")).ToLower();
                    isContainsNguonNhap = dataNguonNhap.Contains(dataOfCell);
                    if (isContainsNguonNhap == true)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "NguonNhap")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Ten SP
                    bool isContainsTenSP = false;
                    string dataTenSP = Convert.ToString(gridView3.GetRowCellValue(e.RowHandle, "TenSanPham")).ToLower();
                    isContainsTenSP = dataTenSP.Contains(dataOfCell);
                    if (isContainsTenSP == true)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "TenSanPham")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("gridView3_CustomDrawCell", ex);
            }
        }
        #endregion

        #region "[Đánh số thứ tự cho dgvThietBiDien và tô màu gridview4]"// Cao Hoang An - 1241301
        private void gridView4_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            try
            {
                if (e.Column.Name == "clSTT4")
                    e.DisplayText = (e.RowHandle + 1).ToString();
                if (isNumberic == true || cbTimKiem.SelectedIndex != 0)
                {
                    //LoaiSanPhamID
                    string dataLoaiSanPhamID = Convert.ToString(gridView4.GetRowCellValue(e.RowHandle, "LoaiSanPhamID"));
                    if (dataLoaiSanPhamID == cbTimKiem.SelectedIndex.ToString())
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "LoaiSanPhamID")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Gia
                    string dataGia = Convert.ToString(gridView4.GetRowCellValue(e.RowHandle, "Gia"));
                    if (dataGia == dataOfCell)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "Gia")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                }
                if (isNumberic == false)
                {
                    //NguonNhap
                    bool isContainsNguonNhap = false;
                    string dataNguonNhap = Convert.ToString(gridView4.GetRowCellValue(e.RowHandle, "NguonNhap")).ToLower();
                    isContainsNguonNhap = dataNguonNhap.Contains(dataOfCell);
                    if (isContainsNguonNhap == true)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "NguonNhap")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Chat Lieu
                    bool isContainsChatLieu = false;
                    string dataChatLieu = Convert.ToString(gridView4.GetRowCellValue(e.RowHandle, "ChatLieu")).ToLower();
                    isContainsChatLieu = dataChatLieu.Contains(dataOfCell);
                    if (isContainsChatLieu == true)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "ChatLieu")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Thong So Ky Thuat
                    bool isContainsThongSoKyThuat = false;
                    string dataThongSoKyThuat = Convert.ToString(gridView4.GetRowCellValue(e.RowHandle, "ThongSoKyThuat")).ToLower();
                    isContainsThongSoKyThuat = dataThongSoKyThuat.Contains(dataOfCell);
                    if (isContainsThongSoKyThuat == true)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "ThongSoKyThuat")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }

                    //NguonSang
                    bool isContainsNguonSang = false;
                    string dataNguonSang = Convert.ToString(gridView4.GetRowCellValue(e.RowHandle, "NguonSang")).ToLower();
                    isContainsNguonSang = dataNguonSang.Contains(dataOfCell);
                    if (isContainsNguonSang == true)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "NguonSang")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }

                    //TamThuNangLuong
                    bool isContainsTamThuNangLuong = false;
                    string dataTamThuNangLuong = Convert.ToString(gridView4.GetRowCellValue(e.RowHandle, "TamThuNangLuong")).ToLower();
                    isContainsTamThuNangLuong = dataTamThuNangLuong.Contains(dataOfCell);
                    if (isContainsTamThuNangLuong == true)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "TamThuNangLuong")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                    //Ten SP
                    bool isContainsTenSP = false;
                    string dataTenSP = Convert.ToString(gridView4.GetRowCellValue(e.RowHandle, "TenThietBi")).ToLower();
                    isContainsTenSP = dataTenSP.Contains(dataOfCell);
                    if (isContainsTenSP == true)
                    {
                        e.Appearance.BackColor = Color.Pink;
                        if (e.Column.FieldName == "TenThietBi")
                        {
                            e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("gridView4_CustomDrawCell", ex);
            }
        }
        #endregion

        #region "[load danh sách các loại sản phẩm lên cbTimKiem]"// Cao Hoang An - 1241301
        /// <summary>
        /// load danh sách các loại sản phẩm lên cbTimKiem
        /// </summary>
        private void LoadLoaiSanPham()
        {
            try
            {
                LoaiSanPhamDTO lspDTO = new LoaiSanPhamDTO();
                lspDTO.TenLoaiSanPham = "Tất cả";
                cbTimKiem.Properties.Items.Add(lspDTO.TenLoaiSanPham);

                foreach (LoaiSanPhamDTO temp in NangLuongTieuDiem_Management.LoaiSanPham_GetAll())
                {
                    cbTimKiem.Properties.Items.Add(temp.TenLoaiSanPham);
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("LoadLoaiSanPham", ex);
            }
        }
        #endregion

        #region "[load danh sách cac loai san pham len combobox cua gridview]"// Cao Hoang An - 1241301
        /// <summary>
        /// load danh sách cac loai san pham len combobox cua gridview
        /// </summary>
        private void LoadComboxLoaiSP()
        {
            try{
            repositoryItemLookUpEdit1.DataSource = repositoryItemLookUpEdit2.DataSource = repositoryItemLookUpEdit3.DataSource = repositoryItemLookUpEdit4.DataSource = NangLuongTieuDiem_Management.LoaiSanPham_GetAll();
            repositoryItemLookUpEdit1.DisplayMember = repositoryItemLookUpEdit2.DisplayMember = repositoryItemLookUpEdit3.DisplayMember = repositoryItemLookUpEdit4.DisplayMember = "TenLoaiSanPham";
            repositoryItemLookUpEdit1.ValueMember = repositoryItemLookUpEdit2.ValueMember = repositoryItemLookUpEdit3.ValueMember = repositoryItemLookUpEdit4.ValueMember = "ID";

            repositoryItemLookUpEdit1.PopulateColumns();
            repositoryItemLookUpEdit1.Columns["LoaiSanPhamChinhID"].Visible = false;
            repositoryItemLookUpEdit1.Columns["Xoa"].Visible = false;
            repositoryItemLookUpEdit1.Columns["ID"].Visible = false;
            repositoryItemLookUpEdit2.PopulateColumns();
            repositoryItemLookUpEdit2.Columns["LoaiSanPhamChinhID"].Visible = false;
            repositoryItemLookUpEdit2.Columns["Xoa"].Visible = false;
            repositoryItemLookUpEdit2.Columns["ID"].Visible = false;
            repositoryItemLookUpEdit3.PopulateColumns();
            repositoryItemLookUpEdit3.Columns["LoaiSanPhamChinhID"].Visible = false;
            repositoryItemLookUpEdit3.Columns["Xoa"].Visible = false;
            repositoryItemLookUpEdit3.Columns["ID"].Visible = false;
            repositoryItemLookUpEdit4.PopulateColumns();
            repositoryItemLookUpEdit4.Columns["LoaiSanPhamChinhID"].Visible = false;
            repositoryItemLookUpEdit4.Columns["Xoa"].Visible = false;
            repositoryItemLookUpEdit4.Columns["ID"].Visible = false;
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("LoadComboxLoaiSP", ex);
            }
        }
        #endregion

        #region "[load danh sách tat ca sản phẩm lên gridView]"// Cao Hoang An - 1241301
        /// <summary>
        /// load danh sách tat ca sản phẩm lên gridView
        /// </summary>
        private void LoadSanPham()
        {
            try
            {
                List<SanPhamDTO> listAll = NangLuongTieuDiem_Management.SanPham_GetAll();
                List<LoaiSanPhamDTO> listLoaiSanPham1 = NangLuongTieuDiem_Management.LoaiSanPham_GetAll().Where(p => p.LoaiSanPhamChinhID == 1).ToList();
                List<LoaiSanPhamDTO> listLoaiSanPham2 = NangLuongTieuDiem_Management.LoaiSanPham_GetAll().Where(p => p.LoaiSanPhamChinhID == 2).ToList();
                List<LoaiSanPhamDTO> listLoaiSanPham3 = NangLuongTieuDiem_Management.LoaiSanPham_GetAll().Where(p => p.LoaiSanPhamChinhID == 3).ToList();
                List<LoaiSanPhamDTO> listLoaiSanPham4 = NangLuongTieuDiem_Management.LoaiSanPham_GetAll().Where(p => p.LoaiSanPhamChinhID == 4).ToList();
                List<SanPhamDTO> list1 = new List<SanPhamDTO>();
                List<SanPhamDTO> list2 = new List<SanPhamDTO>();
                List<SanPhamDTO> list3 = new List<SanPhamDTO>();
                List<SanPhamDTO> list4 = new List<SanPhamDTO>();
                if (listAll.Count > 0)
                {
                    foreach (SanPhamDTO spDto in listAll)
                    {
                        bool flag = false;
                        foreach (LoaiSanPhamDTO lspDto in listLoaiSanPham1)
                        {
                            if (flag == true) break;
                            if (spDto.LoaiSanPhamID == lspDto.ID)
                            {
                                list1.Add(spDto);
                                flag = true;
                                break;
                            }
                        }
                        foreach (LoaiSanPhamDTO lspDto in listLoaiSanPham2)
                        {
                            if (flag == true) break;
                            if (spDto.LoaiSanPhamID == lspDto.ID)
                            {
                                list2.Add(spDto);
                                flag = true;
                                break;
                            }
                        }
                        foreach (LoaiSanPhamDTO lspDto in listLoaiSanPham3)
                        {
                            if (flag == true) break;
                            if (spDto.LoaiSanPhamID == lspDto.ID)
                            {
                                list3.Add(spDto);
                                flag = true;
                                break;
                            }
                        }
                        foreach (LoaiSanPhamDTO lspDto in listLoaiSanPham4)
                        {
                            if (flag == true) break;
                            if (spDto.LoaiSanPhamID == lspDto.ID)
                            {
                                list4.Add(spDto);
                                flag = true;
                                break;
                            }
                        }
                    }
                    if (list1.Count() > 0)
                    {
                        dgvPin.DataSource = list1;
                        gridView1.GroupPanelText = string.Format("Danh sách Pin: Tổng cộng {0} sản phẩm", list1.Count());
                    }
                    else
                    {
                        gridView1.GroupPanelText = "Danh sách Pin: Không tìm thấy dữ liệu.";
                    }
                    if (list2.Count() > 0)
                    {
                        dgvAcQuy.DataSource = list2;
                        gridView2.GroupPanelText = string.Format("Danh sách Ác Quy: Tổng cộng {0} sản phẩm", list2.Count());
                    }
                    else
                    {
                        gridView2.GroupPanelText = "Danh sách Ác Quy: Không tìm thấy dữ liệu.";
                    }
                    if (list3.Count() > 0)
                    {
                        dgvBoDieuKhienSac.DataSource = list3;
                        gridView3.GroupPanelText = string.Format("Danh sách Bộ Điều Khiển Sạc: Tổng cộng {0} sản phẩm", list3.Count());
                    }
                    else
                    {
                        gridView3.GroupPanelText = "Danh sách Bộ Điều Khiển Sạc: Không tìm thấy dữ liệu.";
                    }
                    if (list4.Count() > 0)
                    {
                        //Chuyen doi, Căt thuoc tinh HinhDang ra
                        List<SanPham_ThietBiDienSDK> listTemp = new List<SanPham_ThietBiDienSDK>();
                        SanPham_ThietBiDienSDK sp_tbdSDK;
                        foreach (SanPhamDTO tbDien in list4)
                        {
                            Regex regex = new Regex(@"\w+\W?\s?\w*");
                            MatchCollection mc = regex.Matches(tbDien.HinhDang);

                            sp_tbdSDK = new SanPham_ThietBiDienSDK();
                            sp_tbdSDK.ID = tbDien.ID;
                            sp_tbdSDK.LoaiSanPhamID = tbDien.LoaiSanPhamID;
                            sp_tbdSDK.TenThietBi = tbDien.TenSanPham;
                            sp_tbdSDK.ThongSoKyThuat = mc[0].Value.ToString();
                            sp_tbdSDK.ChatLieu = mc[1].Value.ToString();
                            sp_tbdSDK.NguonSang = mc[2].Value.ToString();
                            sp_tbdSDK.TamThuNangLuong = mc[3].Value.ToString();
                            sp_tbdSDK.NguonNhap = tbDien.NguonNhap;
                            sp_tbdSDK.Gia = tbDien.Gia;
                            sp_tbdSDK.GhiChu = tbDien.GhiChu;
                            listTemp.Add(sp_tbdSDK);
                        }

                        dgvThietBiDien.DataSource = listTemp;
                        gridView4.GroupPanelText = string.Format("Danh sách Thiết Bị Điện: Tổng cộng {0} sản phẩm", list4.Count());
                    }
                    else
                    {
                        gridView4.GroupPanelText = "Danh sách Thiết Bị Điện: Không tìm thấy dữ liệu.";
                    }
                    LoadComboxLoaiSP();
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("LoadSanPham", ex);
            }
        }
        #endregion

        #region "[TimKiem san pham theo LoaiSanPhamID]"// Cao Hoang An - 1241301
        /// <summary>
        /// TimKiem san pham theo LoaiSanPhamID
        /// </summary>
        private void TimKiemTheoLoaiSanPhamID(int LoaiSanPhamID)
        {
            try
            {
                gridView1.GroupPanelText = "Danh sách Pin:";
                gridView2.GroupPanelText = "Danh sách Ác Quy:";
                gridView3.GroupPanelText = "Danh sách Bộ Điều Khiển Sạc:";
                gridView4.GroupPanelText = "Danh sách Thiết Bị Điện:";

                if (LoaiSanPhamID == 0)
                {
                    LoadSanPham();
                }
                else
                {
                    List<SanPhamDTO> list = NangLuongTieuDiem_Management.SelectByLoaiSanPhamID(LoaiSanPhamID);
                    int searchCounter = list.Count();
                    if (list.Count > 0)
                    {
                        List<SanPhamDTO> listAll = NangLuongTieuDiem_Management.SanPham_GetAll();
                        List<LoaiSanPhamDTO> listLoaiSanPham = NangLuongTieuDiem_Management.LoaiSanPham_GetAll().Where(p => p.ID == LoaiSanPhamID).ToList();
                        switch (listLoaiSanPham[0].LoaiSanPhamChinhID)
                        {
                            case 1: //Pin
                                listLoaiSanPham = NangLuongTieuDiem_Management.LoaiSanPham_GetAll().Where(p => p.LoaiSanPhamChinhID == 1).ToList();
                                foreach (SanPhamDTO spDto in listAll)
                                {
                                    bool flag = false;
                                    foreach (LoaiSanPhamDTO lspDto in listLoaiSanPham)
                                    {
                                        if (spDto.LoaiSanPhamID == lspDto.ID)
                                        {
                                            flag = true;
                                            break;
                                        }
                                    }
                                    if (flag == true)
                                    {
                                        if (spDto.LoaiSanPhamID != LoaiSanPhamID)
                                        {
                                            list.Add(spDto);
                                        }
                                    }
                                }
                                dgvPin.DataSource = list;
                                gridView1.GroupPanelText = string.Format("Danh sách Pin. Tìm được {0}/{1} sản phẩm phù hợp với dữ liệu.", searchCounter, list.Count());
                                break;
                            case 2://Ac Quy
                                listLoaiSanPham = NangLuongTieuDiem_Management.LoaiSanPham_GetAll().Where(p => p.LoaiSanPhamChinhID == 2).ToList();
                                foreach (SanPhamDTO spDto in listAll)
                                {
                                    bool flag = false;
                                    foreach (LoaiSanPhamDTO lspDto in listLoaiSanPham)
                                    {
                                        if (spDto.LoaiSanPhamID == lspDto.ID)
                                        {
                                            flag = true;
                                            break;
                                        }
                                    }
                                    if (flag == true)
                                    {
                                        if (spDto.LoaiSanPhamID != LoaiSanPhamID)
                                        {
                                            list.Add(spDto);
                                        }
                                    }
                                }
                                dgvAcQuy.DataSource = list;
                                gridView2.GroupPanelText = string.Format("Danh sách Ác Quy. Tìm được {0}/{1} sản phẩm phù hợp với dữ liệu.", searchCounter, list.Count());
                                break;
                            case 3: //Bo Đieu Kiên Sac
                                listLoaiSanPham = NangLuongTieuDiem_Management.LoaiSanPham_GetAll().Where(p => p.LoaiSanPhamChinhID == 3).ToList();
                                foreach (SanPhamDTO spDto in listAll)
                                {
                                    bool flag = false;
                                    foreach (LoaiSanPhamDTO lspDto in listLoaiSanPham)
                                    {
                                        if (spDto.LoaiSanPhamID == lspDto.ID)
                                        {
                                            flag = true;
                                            break;
                                        }
                                    }
                                    if (flag == true)
                                    {
                                        if (spDto.LoaiSanPhamID != LoaiSanPhamID)
                                        {
                                            list.Add(spDto);
                                        }
                                    }
                                }
                                dgvBoDieuKhienSac.DataSource = list;
                                gridView3.GroupPanelText = string.Format("Danh sách Bộ Điều Khiển Sạc. Tìm được {0}/{1} sản phẩm phù hợp với dữ liệu.", searchCounter, list.Count());
                                break;
                            case 4:
                                listLoaiSanPham = NangLuongTieuDiem_Management.LoaiSanPham_GetAll().Where(p => p.LoaiSanPhamChinhID == 4).ToList();
                                foreach (SanPhamDTO spDto in listAll)
                                {
                                    bool flag = false;
                                    foreach (LoaiSanPhamDTO lspDto in listLoaiSanPham)
                                    {
                                        if (spDto.LoaiSanPhamID == lspDto.ID)
                                        {
                                            flag = true;
                                            break;
                                        }
                                    }
                                    if (flag == true)
                                    {
                                        if (spDto.LoaiSanPhamID != LoaiSanPhamID)
                                        {
                                            list.Add(spDto);
                                        }
                                    }
                                }

                                //Chuyen doi, Căt thuoc tinh HinhDang ra
                                List<SanPham_ThietBiDienSDK> listTemp = new List<SanPham_ThietBiDienSDK>();
                                SanPham_ThietBiDienSDK sp_tbdSDK;
                                foreach (SanPhamDTO tbDien in list)
                                {
                                    Regex regex = new Regex(@"\w+\W?\s?\w*");
                                    MatchCollection mc = regex.Matches(tbDien.HinhDang);

                                    sp_tbdSDK = new SanPham_ThietBiDienSDK();
                                    sp_tbdSDK.ID = tbDien.ID;
                                    sp_tbdSDK.LoaiSanPhamID = tbDien.LoaiSanPhamID;
                                    sp_tbdSDK.TenThietBi = tbDien.TenSanPham;
                                    sp_tbdSDK.ThongSoKyThuat = mc[0].Value.ToString();
                                    sp_tbdSDK.ChatLieu = mc[1].Value.ToString();
                                    sp_tbdSDK.NguonSang = mc[2].Value.ToString();
                                    sp_tbdSDK.TamThuNangLuong = mc[3].Value.ToString();
                                    sp_tbdSDK.NguonNhap = tbDien.NguonNhap;
                                    sp_tbdSDK.Gia = tbDien.Gia;
                                    sp_tbdSDK.GhiChu = tbDien.GhiChu;
                                    listTemp.Add(sp_tbdSDK);
                                }

                                dgvThietBiDien.DataSource = listTemp;
                                gridView4.GroupPanelText = string.Format("Danh sách Thiết Bị Điện. Tìm được {0}/{1} sản phẩm phù hợp với dữ liệu.", searchCounter, list.Count());
                                break;
                            default:
                                break;
                        }
                        LoadComboxLoaiSP();
                    }
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("TimKiemTheoLoaiSanPhamID", ex);
            }
        }
        #endregion

        #region "[TimKiem san pham theo nguoi dung nhap vao]"// Cao Hoang An - 1241301
        /// <summary>
        /// TimKiem san pham theo nguoi dung nhap vao
        /// </summary>
        private void TimKiemTheoNguoiDungNhapVao(bool flagNumberic, object Data)
        {
            try
            {
                List<SanPhamDTO> listAll = NangLuongTieuDiem_Management.SanPham_GetAll();

                //Pin
                List<SanPhamDTO> listPin = NangLuongTieuDiem_Management.SanPham_SelectByMulti(flagNumberic, Data, 1);
                int searchCounterPin = listPin.Count();
                if (searchCounterPin > 0)
                {
                    List<LoaiSanPhamDTO> listLoaiSanPham = NangLuongTieuDiem_Management.LoaiSanPham_GetAll().Where(p => p.LoaiSanPhamChinhID == 1).ToList();

                    foreach (SanPhamDTO spDto in listAll)
                    {
                        bool flag = false;
                        foreach (LoaiSanPhamDTO lspDto in listLoaiSanPham)
                        {
                            if (spDto.LoaiSanPhamID == lspDto.ID)
                            {
                                flag = true;
                                break;
                            }
                        }
                        if (flag == true)
                        {
                            if (isNumberic == false)
                            {
                                if (!spDto.TenSanPham.ToLower().Contains(dataOfCell) &&
                                !spDto.HinhDang.ToLower().Contains(dataOfCell) &&
                                !spDto.NguonNhap.ToLower().Contains(dataOfCell))
                                {
                                    listPin.Add(spDto);
                                }
                            }
                            else
                            {
                                if (spDto.CongSuat.ToString() != dataOfCell &&
                                    spDto.Dai.ToString() != dataOfCell &&
                                    spDto.Rong.ToString() != dataOfCell &&
                                    spDto.Day.ToString() != dataOfCell &&
                                    spDto.Imp.ToString() != dataOfCell &&
                                    spDto.Vmp.ToString() != dataOfCell &&
                                    spDto.Isc.ToString() != dataOfCell &&
                                    spDto.Voc.ToString() != dataOfCell &&
                                    spDto.Gia.ToString() != dataOfCell)
                                {
                                    listPin.Add(spDto);
                                }
                            }
                        }
                        dgvPin.DataSource = listPin;
                        gridView1.GroupPanelText = string.Format("Danh sách Pin: Tìm được {0}/{1} sản phẩm phù hợp với dữ liệu.", searchCounterPin, listPin.Count());
                    }
                }
                else
                {
                    gridView1.GroupPanelText = "Danh sách Pin: Không tìm thấy dữ liệu.";
                }

                //AcQuy
                List<SanPhamDTO> listAcQuy = NangLuongTieuDiem_Management.SanPham_SelectByMulti(flagNumberic, Data, 2);
                int searchCounterAcQuy = listAcQuy.Count();
                if (searchCounterAcQuy > 0)
                {
                    List<LoaiSanPhamDTO> listLoaiSanPham = NangLuongTieuDiem_Management.LoaiSanPham_GetAll().Where(p => p.LoaiSanPhamChinhID == 2).ToList();

                    foreach (SanPhamDTO spDto in listAll)
                    {
                        bool flag = false;
                        foreach (LoaiSanPhamDTO lspDto in listLoaiSanPham)
                        {
                            if (spDto.LoaiSanPhamID == lspDto.ID)
                            {
                                flag = true;
                                break;
                            }
                        }
                        if (flag == true)
                        {
                            if (isNumberic == false)
                            {
                                if (!spDto.TenSanPham.ToLower().Contains(dataOfCell) &&
                                !spDto.NguonNhap.ToLower().Contains(dataOfCell))
                                {
                                    listAcQuy.Add(spDto);
                                }
                            }
                            else
                            {
                                if (spDto.CongSuat.ToString() != dataOfCell &&
                                    spDto.Vmp.ToString() != dataOfCell &&
                                    spDto.Gia.ToString() != dataOfCell)
                                {
                                    listAcQuy.Add(spDto);
                                }
                            }
                        }
                        dgvAcQuy.DataSource = listAcQuy;
                        gridView2.GroupPanelText = string.Format("Danh sách Ác Quy: Tìm được {0}/{1} sản phẩm phù hợp với dữ liệu.", searchCounterAcQuy, listAcQuy.Count());
                    }
                }
                else
                {
                    gridView2.GroupPanelText = "Danh sách Ác Quy: Không tìm thấy dữ liệu.";
                }

                //BoDieuKienSac
                List<SanPhamDTO> listBoDieuKienSac = NangLuongTieuDiem_Management.SanPham_SelectByMulti(flagNumberic, Data, 3);
                int searchCounterBoDieuKienSac = listBoDieuKienSac.Count();
                if (searchCounterBoDieuKienSac > 0)
                {
                    List<LoaiSanPhamDTO> listLoaiSanPham = NangLuongTieuDiem_Management.LoaiSanPham_GetAll().Where(p => p.LoaiSanPhamChinhID == 3).ToList();

                    foreach (SanPhamDTO spDto in listAll)
                    {
                        bool flag = false;
                        foreach (LoaiSanPhamDTO lspDto in listLoaiSanPham)
                        {
                            if (spDto.LoaiSanPhamID == lspDto.ID)
                            {
                                flag = true;
                                break;
                            }
                        }
                        if (flag == true)
                        {
                            if (isNumberic == false)
                            {
                                if (!spDto.TenSanPham.ToLower().Contains(dataOfCell) &&
                                !spDto.NguonNhap.ToLower().Contains(dataOfCell))
                                {
                                    listBoDieuKienSac.Add(spDto);
                                }
                            }
                            else
                            {
                                if (spDto.Imp.ToString() != dataOfCell &&
                                    spDto.Vmp.ToString() != dataOfCell &&
                                    spDto.Voc.ToString() != dataOfCell &&
                                    spDto.Gia.ToString() != dataOfCell)
                                {
                                    listBoDieuKienSac.Add(spDto);
                                }
                            }
                        }
                        dgvBoDieuKhienSac.DataSource = listBoDieuKienSac;
                        gridView3.GroupPanelText = string.Format("Danh sách Bộ Điều Khiển Sạc: Tìm được {0}/{1} sản phẩm phù hợp với dữ liệu.", searchCounterBoDieuKienSac, listBoDieuKienSac.Count());
                    }
                }
                else
                {
                    gridView3.GroupPanelText = "Danh sách Bộ Điều Khiển Sạc: Không tìm thấy dữ liệu.";
                }

                //Thiet Bi Dien
                List<SanPhamDTO> listThietBiDien = NangLuongTieuDiem_Management.SanPham_SelectByMulti(flagNumberic, Data, 4);
                int searchCounterThietBiDien = listThietBiDien.Count();
                if (searchCounterThietBiDien > 0)
                {
                    List<LoaiSanPhamDTO> listLoaiSanPham = NangLuongTieuDiem_Management.LoaiSanPham_GetAll().Where(p => p.LoaiSanPhamChinhID == 4).ToList();

                    foreach (SanPhamDTO spDto in listAll)
                    {
                        bool flag = false;
                        foreach (LoaiSanPhamDTO lspDto in listLoaiSanPham)
                        {
                            if (spDto.LoaiSanPhamID == lspDto.ID)
                            {
                                flag = true;
                                break;
                            }
                        }
                        if (flag == true)
                        {
                            if (isNumberic == false)
                            {
                                if (!spDto.TenSanPham.ToLower().Contains(dataOfCell) &&
                                !spDto.HinhDang.ToLower().Contains(dataOfCell) &&
                                !spDto.NguonNhap.ToLower().Contains(dataOfCell))
                                {
                                    listThietBiDien.Add(spDto);
                                }
                            }
                            else
                            {
                                if (spDto.Gia.ToString() != dataOfCell)
                                {
                                    listThietBiDien.Add(spDto);
                                }
                            }
                        }

                        //Chuyen doi, Căt thuoc tinh HinhDang ra
                        List<SanPham_ThietBiDienSDK> list = new List<SanPham_ThietBiDienSDK>();
                        SanPham_ThietBiDienSDK sp_tbdSDK;
                        foreach (SanPhamDTO tbDien in listThietBiDien)
                        {
                            Regex regex = new Regex(@"\w+\W?\s?\w*");
                            MatchCollection mc = regex.Matches(tbDien.HinhDang);

                            sp_tbdSDK = new SanPham_ThietBiDienSDK();
                            sp_tbdSDK.ID = tbDien.ID;
                            sp_tbdSDK.LoaiSanPhamID = tbDien.LoaiSanPhamID;
                            sp_tbdSDK.TenThietBi = tbDien.TenSanPham;
                            sp_tbdSDK.ThongSoKyThuat = mc[0].Value.ToString();
                            sp_tbdSDK.ChatLieu = mc[1].Value.ToString();
                            sp_tbdSDK.NguonSang = mc[2].Value.ToString();
                            sp_tbdSDK.TamThuNangLuong = mc[3].Value.ToString();
                            sp_tbdSDK.NguonNhap = tbDien.NguonNhap;
                            sp_tbdSDK.Gia = tbDien.Gia;
                            sp_tbdSDK.GhiChu = tbDien.GhiChu;
                            list.Add(sp_tbdSDK);
                        }
                        dgvThietBiDien.DataSource = list;
                        gridView4.GroupPanelText = string.Format("Danh sách Thiết Bị Điện: Tìm được {0}/{1} sản phẩm phù hợp với dữ liệu.", searchCounterThietBiDien, listThietBiDien.Count());
                    }
                }
                else
                {
                    gridView4.GroupPanelText = "Danh sách Thiết Bị Điện: Không tìm thấy dữ liệu.";
                }
                LoadComboxLoaiSP();
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("TimKiemTheoNguoiDungNhapVao", ex);
            }
        }
        #endregion

        #region "[su kien tao menu khi right click vao gridview1]"// Cao Hoang An - 1241301
        /// <summary>
        /// su kien tao menu khi right click vao gridview1
        /// </summary>
        private void gridView1_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            try
            {
                GridView view = sender as GridView;
                if (e.MenuType == GridMenuType.Row)
                {
                    int rowHandle = e.HitInfo.RowHandle;
                    // Delete existing menu items, if any.
                    e.Menu.Items.Clear();

                    DXMenuItem menuItem;

                    menuItem = new DXMenuItem("&Cập Nhật", new EventHandler(OnUpdateRowClick1));
                    menuItem.Tag = new RowInfo(view, rowHandle);
                    e.Menu.Items.Add(menuItem);

                    menuItem = new DXMenuItem("&Xóa", new EventHandler(OnDeleteRowClick1));
                    menuItem.Tag = new RowInfo(view, rowHandle);
                    e.Menu.Items.Add(menuItem);
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("gridView1_PopupMenuShowing", ex);
            }
        }
        #endregion

        #region "[su kien tao menu khi right click vao gridview2]"// Cao Hoang An - 1241301
        /// <summary>
        /// su kien tao menu khi right click vao gridview2
        /// </summary>
        private void gridView2_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
        {
            try
            {
                GridView view = sender as GridView;
                if (e.MenuType == GridMenuType.Row)
                {
                    int rowHandle = e.HitInfo.RowHandle;
                    // Delete existing menu items, if any.
                    e.Menu.Items.Clear();

                    DXMenuItem menuItem;

                    menuItem = new DXMenuItem("&Cập Nhật", new EventHandler(OnUpdateRowClick2));
                    menuItem.Tag = new RowInfo(view, rowHandle);
                    e.Menu.Items.Add(menuItem);

                    menuItem = new DXMenuItem("&Xóa", new EventHandler(OnDeleteRowClick2));
                    menuItem.Tag = new RowInfo(view, rowHandle);
                    e.Menu.Items.Add(menuItem);
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("gridView2_PopupMenuShowing", ex);
            }
        }
        #endregion

        #region "[su kien tao menu khi right click vao gridview3]"// Cao Hoang An - 1241301
        /// <summary>
        /// su kien tao menu khi right click vao gridview3
        /// </summary>
        private void gridView3_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
        {
            try
            {
                GridView view = sender as GridView;
                if (e.MenuType == GridMenuType.Row)
                {
                    int rowHandle = e.HitInfo.RowHandle;
                    // Delete existing menu items, if any.
                    e.Menu.Items.Clear();

                    DXMenuItem menuItem;

                    menuItem = new DXMenuItem("&Cập Nhật", new EventHandler(OnUpdateRowClick3));
                    menuItem.Tag = new RowInfo(view, rowHandle);
                    e.Menu.Items.Add(menuItem);

                    menuItem = new DXMenuItem("&Xóa", new EventHandler(OnDeleteRowClick3));
                    menuItem.Tag = new RowInfo(view, rowHandle);
                    e.Menu.Items.Add(menuItem);
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("gridView3_PopupMenuShowing", ex);
            }
        }
        #endregion

        #region "[su kien tao menu khi right click vao gridview4]"// Cao Hoang An - 1241301
        /// <summary>
        /// su kien tao menu khi right click vao gridview4
        /// </summary>
        private void gridView4_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
        {
            try
            {
                GridView view = sender as GridView;
                if (e.MenuType == GridMenuType.Row)
                {
                    int rowHandle = e.HitInfo.RowHandle;
                    // Delete existing menu items, if any.
                    e.Menu.Items.Clear();

                    DXMenuItem menuItem;

                    menuItem = new DXMenuItem("&Cập Nhật", new EventHandler(OnUpdateRowClick4));
                    menuItem.Tag = new RowInfo(view, rowHandle);
                    e.Menu.Items.Add(menuItem);

                    menuItem = new DXMenuItem("&Xóa", new EventHandler(OnDeleteRowClick4));
                    menuItem.Tag = new RowInfo(view, rowHandle);
                    e.Menu.Items.Add(menuItem);
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("gridView4_PopupMenuShowing", ex);
            }
        }
        #endregion

        #region "[handler CapNhat 1 dong hien tai gridview1]"// Cao Hoang An - 1241301
        /// <summary>
        /// handler CapNhat 1 dong hien tai gridview1
        /// </summary>
        void OnUpdateRowClick1(object sender, EventArgs e)
        {
            try
            {
                DXMenuItem item = sender as DXMenuItem;
                RowInfo info = item.Tag as RowInfo;

                SanPhamDTO spDto = new SanPhamDTO();
                spDto.ID = Convert.ToInt32(gridView1.GetRowCellValue(info.RowHandle, "ID"));
                spDto.TenSanPham = gridView1.GetRowCellValue(info.RowHandle, "TenSanPham").ToString();
                spDto.LoaiSanPhamID = Convert.ToInt32(gridView1.GetRowCellValue(info.RowHandle, "LoaiSanPhamID"));
                spDto.CongSuat = Convert.ToDouble(gridView1.GetRowCellValue(info.RowHandle, "CongSuat"));
                spDto.HinhDang = gridView1.GetRowCellValue(info.RowHandle, "HinhDang").ToString();
                spDto.Dai = Convert.ToDouble(gridView1.GetRowCellValue(info.RowHandle, "Dai"));
                spDto.Rong = Convert.ToDouble(gridView1.GetRowCellValue(info.RowHandle, "Rong"));
                spDto.Day = Convert.ToDouble(gridView1.GetRowCellValue(info.RowHandle, "Day"));
                spDto.Imp = Convert.ToDouble(gridView1.GetRowCellValue(info.RowHandle, "Imp"));
                spDto.Vmp = Convert.ToDouble(gridView1.GetRowCellValue(info.RowHandle, "Vmp"));
                spDto.Voc = Convert.ToDouble(gridView1.GetRowCellValue(info.RowHandle, "Voc"));
                spDto.Isc = Convert.ToDouble(gridView1.GetRowCellValue(info.RowHandle, "Isc"));
                spDto.NguonNhap = gridView1.GetRowCellValue(info.RowHandle, "NguonNhap").ToString();
                spDto.Gia = Convert.ToDouble(gridView1.GetRowCellValue(info.RowHandle, "Gia"));
                spDto.GhiChu = gridView1.GetRowCellValue(info.RowHandle, "GhiChu").ToString();
                NangLuongTieuDiem_Management.SanPham_Update(spDto);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("OnUpdateRowClick1", ex);
            }
        }
        #endregion

        #region "[handler CapNhat 1 dong hien tai gridview2]"// Cao Hoang An - 1241301
        /// <summary>
        /// handler CapNhat 1 dong hien tai gridview2
        /// </summary>
        void OnUpdateRowClick2(object sender, EventArgs e)
        {
            try
            {
                DXMenuItem item = sender as DXMenuItem;
                RowInfo info = item.Tag as RowInfo;

                SanPhamDTO spDto = new SanPhamDTO();
                spDto.ID = Convert.ToInt32(gridView2.GetRowCellValue(info.RowHandle, "ID"));
                spDto.TenSanPham = gridView2.GetRowCellValue(info.RowHandle, "TenSanPham").ToString();
                spDto.LoaiSanPhamID = Convert.ToInt32(gridView2.GetRowCellValue(info.RowHandle, "LoaiSanPhamID"));
                spDto.CongSuat = Convert.ToDouble(gridView2.GetRowCellValue(info.RowHandle, "CongSuat"));
                spDto.Vmp = Convert.ToDouble(gridView2.GetRowCellValue(info.RowHandle, "Vmp"));
                spDto.NguonNhap = gridView2.GetRowCellValue(info.RowHandle, "NguonNhap").ToString();
                spDto.Gia = Convert.ToDouble(gridView2.GetRowCellValue(info.RowHandle, "Gia"));
                spDto.GhiChu = gridView2.GetRowCellValue(info.RowHandle, "GhiChu").ToString();
                NangLuongTieuDiem_Management.SanPham_Update(spDto);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("OnUpdateRowClick2", ex);
            }
        }
        #endregion

        #region "[handler CapNhat 1 dong hien tai gridview3]"// Cao Hoang An - 1241301
        /// <summary>
        /// handler CapNhat 1 dong hien tai gridview3
        /// </summary>
        void OnUpdateRowClick3(object sender, EventArgs e)
        {
            try
            {
                DXMenuItem item = sender as DXMenuItem;
                RowInfo info = item.Tag as RowInfo;

                SanPhamDTO spDto = new SanPhamDTO();
                spDto.ID = Convert.ToInt32(gridView3.GetRowCellValue(info.RowHandle, "ID"));
                spDto.TenSanPham = gridView3.GetRowCellValue(info.RowHandle, "TenSanPham").ToString();
                spDto.LoaiSanPhamID = Convert.ToInt32(gridView3.GetRowCellValue(info.RowHandle, "LoaiSanPhamID"));
                spDto.Imp = Convert.ToDouble(gridView3.GetRowCellValue(info.RowHandle, "Imp"));
                spDto.Vmp = Convert.ToDouble(gridView3.GetRowCellValue(info.RowHandle, "Vmp"));
                spDto.Voc = Convert.ToDouble(gridView3.GetRowCellValue(info.RowHandle, "Voc"));
                spDto.NguonNhap = gridView3.GetRowCellValue(info.RowHandle, "NguonNhap").ToString();
                spDto.Gia = Convert.ToDouble(gridView3.GetRowCellValue(info.RowHandle, "Gia"));
                spDto.GhiChu = gridView3.GetRowCellValue(info.RowHandle, "GhiChu").ToString();
                NangLuongTieuDiem_Management.SanPham_Update(spDto);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("OnUpdateRowClick3", ex);
            }
        }
        #endregion

        #region "[handler CapNhat 1 dong hien tai gridview4]"// Cao Hoang An - 1241301
        /// <summary>
        /// handler CapNhat 1 dong hien tai gridview4
        /// </summary>
        void OnUpdateRowClick4(object sender, EventArgs e)
        {
            try
            {
                DXMenuItem item = sender as DXMenuItem;
                RowInfo info = item.Tag as RowInfo;

                SanPhamDTO spDto = new SanPhamDTO();
                spDto.ID = Convert.ToInt32(gridView4.GetRowCellValue(info.RowHandle, "ID"));
                spDto.TenSanPham = gridView4.GetRowCellValue(info.RowHandle, "TenThietBi").ToString();
                spDto.LoaiSanPhamID = Convert.ToInt32(gridView4.GetRowCellValue(info.RowHandle, "LoaiSanPhamID"));
                string HinhDang = gridView4.GetRowCellValue(info.RowHandle, "ThongSoKyThuat").ToString();
                HinhDang += " * " + gridView4.GetRowCellValue(info.RowHandle, "ChatLieu").ToString();
                HinhDang += " * " + gridView4.GetRowCellValue(info.RowHandle, "NguonSang").ToString();
                HinhDang += " * " + gridView4.GetRowCellValue(info.RowHandle, "TamThuNangLuong").ToString();
                spDto.HinhDang = HinhDang;
                spDto.NguonNhap = gridView4.GetRowCellValue(info.RowHandle, "NguonNhap").ToString();
                spDto.Gia = Convert.ToDouble(gridView4.GetRowCellValue(info.RowHandle, "Gia"));
                spDto.GhiChu = gridView4.GetRowCellValue(info.RowHandle, "GhiChu").ToString();
                NangLuongTieuDiem_Management.SanPham_Update(spDto);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("OnUpdateRowClick4", ex);
            }
        }
        #endregion

        #region "[handler Xoa 1 dong hien tai gridview1]"// Cao Hoang An - 1241301
        /// <summary>
        /// handler Xoa 1 dong hien tai gridview1
        /// </summary>
        void OnDeleteRowClick1(object sender, EventArgs e)
        {
            try
            {
                DXMenuItem item = sender as DXMenuItem;
                RowInfo info = item.Tag as RowInfo;
                NangLuongTieuDiem_Management.SanPham_Delete(Convert.ToInt32(gridView1.GetRowCellValue(info.RowHandle, "ID")));
                info.View.DeleteRow(info.RowHandle);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("OnDeleteRowClick1", ex);
            }
        }
        #endregion

        #region "[handler Xoa 1 dong hien tai gridview2]"// Cao Hoang An - 1241301
        /// <summary>
        /// handler Xoa 1 dong hien tai gridview2
        /// </summary>
        void OnDeleteRowClick2(object sender, EventArgs e)
        {
            try
            {
                DXMenuItem item = sender as DXMenuItem;
                RowInfo info = item.Tag as RowInfo;
                NangLuongTieuDiem_Management.SanPham_Delete(Convert.ToInt32(gridView2.GetRowCellValue(info.RowHandle, "ID")));
                info.View.DeleteRow(info.RowHandle);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("OnDeleteRowClick2", ex);
            }
        }
        #endregion

        #region "[handler Xoa 1 dong hien tai gridview3]"// Cao Hoang An - 1241301
        /// <summary>
        /// handler Xoa 1 dong hien tai gridview3
        /// </summary>
        void OnDeleteRowClick3(object sender, EventArgs e)
        {
            try
            {
                DXMenuItem item = sender as DXMenuItem;
                RowInfo info = item.Tag as RowInfo;
                NangLuongTieuDiem_Management.SanPham_Delete(Convert.ToInt32(gridView3.GetRowCellValue(info.RowHandle, "ID")));
                info.View.DeleteRow(info.RowHandle);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("OnDeleteRowClick3", ex);
            }
        }
        #endregion

        #region "[handler Xoa 1 dong hien tai gridview4]"// Cao Hoang An - 1241301
        /// <summary>
        /// handler Xoa 1 dong hien tai gridview4
        /// </summary>
        void OnDeleteRowClick4(object sender, EventArgs e)
        {
            try
            {
                DXMenuItem item = sender as DXMenuItem;
                RowInfo info = item.Tag as RowInfo;
                NangLuongTieuDiem_Management.SanPham_Delete(Convert.ToInt32(gridView4.GetRowCellValue(info.RowHandle, "ID")));
                info.View.DeleteRow(info.RowHandle);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("OnDeleteRowClick4", ex);
            }
        }
        #endregion

        #region "[Thong Tin cua dong hien tai]"// Cao Hoang An - 1241301
        /// <summary>
        /// Thong Tin cua dong hien tai
        /// </summary>
        class RowInfo
        {
            public RowInfo(GridView view, int rowHandle)
            {
                this.RowHandle = rowHandle;
                this.View = view;
            }
            public GridView View;
            public int RowHandle;
        }
        #endregion

        #region "[bat loi tren gridView]"// Cao Hoang An - 1241301
        /// <summary>
        /// bat loi tren gridView
        /// </summary>
        private void gridView1_InvalidValueException(object sender, DevExpress.XtraEditors.Controls.InvalidValueExceptionEventArgs e)
        {
            try
            {
                e.ExceptionMode = ExceptionMode.DisplayError;
                e.WindowCaption = "Input Error";
                e.ErrorText = "Bạn phải nhập số ở ô này.";
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("gridView1_InvalidValueException", ex);
            }
        }
        #endregion

        #region "[btnXuatFileExcel_Click]"// Cao Hoang An - 1241301
        /// <summary>
        /// btnXuatFileExcel_Click
        /// </summary>
        private void btnXuatFileExcel_Click(object sender, EventArgs e)
        {
            try
            {
                SaveFileDialog saveDiaLog = new SaveFileDialog();
                saveDiaLog.Filter = "Excel Files (*.xls)|*.xls|All files (*)|*.*";
                saveDiaLog.FileName = "NangLuongTieuDiem";
                if (DialogResult.OK == saveDiaLog.ShowDialog())
                {
                    string fileName = Path.GetFileNameWithoutExtension(saveDiaLog.FileName);
                    string fileDirectory = Path.GetDirectoryName(saveDiaLog.FileName);
                    string filePath = fileDirectory;

                    if (dgvPin.DataSource == null && dgvAcQuy.DataSource == null && dgvBoDieuKhienSac.DataSource == null && dgvThietBiDien.DataSource == null)
                    {
                        MessageBox.Show("Không thể xuất ra file excel vì hiện tại không tìm thấy danh sách sản phẩm bạn cần tìm.", "Thông báo:");
                        return;
                    }
                    if (dgvPin.DataSource != null)
                    {
                        filePath = fileDirectory + @"\" + fileName + "_DanhSachPin_" + DateTime.Now.ToString("dd.MM.yyyy") + ".xls";
                        dgvPin.ExportToXls(filePath);
                    }
                    if (dgvAcQuy.DataSource != null)
                    {
                        filePath = fileDirectory + @"\" + fileName + "_DanhSachAcQuy_" + DateTime.Now.ToString("dd.MM.yyyy") + ".xls";
                        dgvAcQuy.ExportToXls(filePath);
                    }
                    if (dgvBoDieuKhienSac.DataSource != null)
                    {
                        filePath = fileDirectory + @"\" + fileName + "_DanhSachBoDieuKhienSac_" + DateTime.Now.ToString("dd.MM.yyyy") + ".xls";
                        dgvBoDieuKhienSac.ExportToXls(filePath);
                    }
                    if (dgvThietBiDien.DataSource != null)
                    {
                        filePath = fileDirectory + @"\" + fileName + "_DanhSachThietBiDien_" + DateTime.Now.ToString("dd.MM.yyyy") + ".xls";
                        dgvThietBiDien.ExportToXls(filePath);
                    }
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("btnXuatFileExcel_Click", ex);
            }
        }
        #endregion

        #region "[Hàm insert lỗi hệ thống]"
        public void InSert_LoiHeThong(string TenHam, Exception ex)
        {
            LoiHeThongDTO lhtDto = new LoiHeThongDTO();
            lhtDto.TenLoi = ex.Message.ToString();
            lhtDto.NgayTao = DateTime.Now;
            lhtDto.TenHamBiLoi = "ucDanhSachSanPham_" + TenHam;
            lhtDto.SuaLoi = false;
            lhtDto.GhiChu = "";
            NangLuongTieuDiem_Management.LoiHeThong_Insert(lhtDto);
            MessageBox.Show(lhtDto.TenLoi);
        }
        #endregion 
    }
}
